package com.winglish.as3.exam.toeicspeaking 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.external.ExternalInterface;
	
	import com.winglish.as3.exam.toeicspeaking.impl.TSPart;
	import com.winglish.as3.utils.StringUtil;
	
	/**
	 * ...
	 * @author charmjin
	 */
	public class TSSingle extends TSCommon
	{
		private var dataObj:Object;
		private var part:TSPart;
		private var current:int = 0;
		
		public function TSSingle(no:int = 0, data:Object = null) 
		{
			if (no && data) {
				super(no);
				dataObj = data;
				
				var sArr:Array = new Array();
				var aArr:Array = new Array();
				var iArr:Array = new Array();
				for (var i:uint = 0; i < dataObj.key.length; i++) {
					var str:String = String(dataObj.key[i]);
					if (str.indexOf("quiz") != -1) {
						if (StringUtil.replaceEmpty(dataObj[dataObj.key[i]].soundQ) != "") sArr.push(dataObj[dataObj.key[i]].soundQ);
						if (StringUtil.replaceEmpty(dataObj[dataObj.key[i]].soundA) != "") aArr.push(dataObj[dataObj.key[i]].soundA);
						if (StringUtil.replaceEmpty(dataObj[dataObj.key[i]].img) != "") iArr= String(dataObj[dataObj.key[i]].img).split(",");
					}
				}
				
				if (int(dataObj.part) >= 1 && int(dataObj.part) <= 6) {
					super.soundLoad(dataObj.soundPath, sArr, aArr);
					super.imgLoad(dataObj.imgPath, iArr);
					addEventListener("soundPlayComplete", soundPlayComplete);
				}else {
					try {
						ExternalInterface.call("alert", "CMS에서 Part 번호를 1~6 사이로 입력해주세요.");
					}catch (e1:Error) {
						throw new Error( "Part 번호를 1~6 사이로 입력해주세요." );
					}
				}
				
			}
		}
		
		/**
		 * sound play complete event
		 * @param	e
		 */
		private function soundPlayComplete(e:Event):void {
			if (super.count == TSData["PART" + super.partNo].length) {	//맨 마지막에 답변듣기 사운드, 녹음 객체 넘기기
				part.soundRecordSet(super.a_sound, super.record);
			}
			part.action(super.count);
			super.mapping(mcParent.mcRoot);
			
		}

		/**
		 * test start
		 * @param	mcParent
		 */
		public function TSStart(mcParent:MovieClip):void {
			if (part == null) {
				switch(uint(dataObj.part)) {
					case 1:
						part = new TSPart1(mcParent, dataObj);
						break;
					case 2:
						part = new TSPart2(mcParent, dataObj, super.img.getImg(0));
						break;
					case 3:
						part = new TSPart3(mcParent, dataObj);
						break;
					case 4:
						part = new TSPart4(mcParent, dataObj, super.img.getImg(0), super.img.getImg(1));
						break;
					case 5:
						part = new TSPart5(mcParent, dataObj);
						break;
					case 6:
						part = new TSPart6(mcParent, dataObj);
						break;
				}
				
				if (!part.hasEventListener("reStart")) part.addEventListener("reStart", reStartSet);
			}
			
			super.tsSoundPlay();
			super.mcParent = mcParent;
			super.goAnswerSet();
		}
		
		/**
		 * 다시풀기 버튼 클릭해서 발생되는 이벤트
		 * @param	e
		 */
		private function reStartSet(e:Event):void {
			super.reset();
			
		}
	}

}